package servlet;

import dao.EmployeeDao;
import dao.impl.EmployeeDaoImpl;
import entity.Employee;
import util.EmployeeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/employee/delete")
public class EmployeeDeleteServlet extends HttpServlet {

    //导入持久层
    private EmployeeDao employeeDao;

    public EmployeeDeleteServlet(){
        this.employeeDao = new EmployeeDaoImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // //1.获取请求参数
        // String id = req.getParameter("id");
        // System.out.println(id);
        // String name = req.getParameter("name");
        // String birthday = req.getParameter("birthday");
        // String gender = req.getParameter("gender");
        // String phone = req.getParameter("phone");
        // //创建employee对象
        // Employee employee = new Employee(Integer.parseInt(id),name,
        //         LocalDate.parse(birthday),Integer.parseInt(gender),phone);
        Employee employee = EmployeeUtil.getEmployeeByID(req);
        try {
            employeeDao.deleteEmployee(employee);
            System.out.println(employee);
            //跳转到哪里？
            req.getRequestDispatcher("/employee").forward(req,resp);
        } catch (Exception e) {
            //跳转到哪里？
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}
